package cl.loso.melon.server.negocio;

import java.util.Date;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import cl.loso.melon.server.model.ItemLVS;
import cl.loso.melon.server.model.NegocioLVS;
import cl.loso.melon.server.persistencia.ItemLVSDAO;
import cl.loso.melon.server.persistencia.NegocioLVSDAO;

public class ItemSVLBO {
	private static Log log = LogFactory.getLog(ItemSVLBO.class);

	public static void guardarItemSVL(String item,String nombre) throws Exception {
		try {
			UserService userService = UserServiceFactory.getUserService();
			User user = userService.getCurrentUser();

			//ItemLVS iLVS=new ItemLVS(user, item.toLowerCase(), new Date());
			ItemLVS iLVS=new ItemLVS(user, item.toLowerCase(), new Date(),nombre);
			ItemLVSDAO.insertar(iLVS);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw e;
		}
	}
	
	public static void actualizarItemSVL(String id,String newItem,String nombre) throws Exception {
		try {
			ItemLVSDAO.actualizar(Long.valueOf(Long.parseLong(id)),newItem,nombre);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw e;
		}
	}	
	
	public static List<ItemLVS> obtenerItemSVL() throws Exception {
		try {
			return ItemLVSDAO.obtener();
		} catch (Exception e) {
			log.error(e.getMessage());
			throw e;
		}
	}
	
	public static void borrarItemSVL(String id) throws Exception {
		try {
			ItemLVSDAO.borrar(Long.valueOf(Long.parseLong(id)));
		} catch (Exception e) {
			log.error(e.getMessage());
			throw e;
		}
	}	
	
	public static ItemLVS editarItemSVL(String id) throws Exception {
		try {
			return ItemLVSDAO.getItemLVSbyId(Long.valueOf(Long.parseLong(id)));
		} catch (Exception e) {
			log.error(e.getMessage());
			throw e;

		}
	}
	
	public static List<ItemLVS> obtenerTemaPorNombreLVS(String item) throws Exception {
		try {
			return ItemLVSDAO.buscarItemPorNombre(item);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw e;
		}
	}	
	
	public static boolean buscarNombre(String nombre){
		List<ItemLVS> itemList= ItemLVSDAO.buscarNombre(nombre.trim());
		if(!itemList.isEmpty()){
			ItemLVS item=itemList.get(0);
			String itemBase=item.getDescripcion().trim().toLowerCase();
			if(nombre.trim().toLowerCase().equals(itemBase)){
				return false;
			}
		}
		return true;
	}	
}
